/**
 * Copyright (C) 2017 - 2020 https://github.com/joewee
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bifrost.repository;

import com.bifrost.entity.Coupon;
import com.bifrost.entity.CouponDetail;
import com.bifrost.entity.Customer;
import com.bifrost.entity.MerchantStore;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Nonnull;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 *
 *
 *@author joewee
 *@version 1.0.0
 *@Date 2017/11/7 19:28
 */
public interface CouponDetailRepository extends JpaRepository<CouponDetail,Long>,JpaSpecificationExecutor<CouponDetail>{

    /**
     * 获取已经使用过的优惠券
     * @param customer
     * @return
     */
    @Query(value = "select  c from CouponDetail c where c.customer =?1 and  c.isUsed =true")
    List<CouponDetail> findAllUsedByCustomer(Customer customer);

    /**
     * 获取会员未使用的优惠券（包括过期、删除、锁定、回滚中状态的优惠券）
     * @param customer
     * @return
     */
    @Query(value = "select c from CouponDetail c where c.customer = ?1 and (c.isUsed is null or c.isUsed=false ) order by c.addTime desc ")
    List<CouponDetail> findAllUnusedByCustomer(Customer customer);

    /**
     * 查询会员指定状态的优惠券
     * @param customer
     * @param status
     * @return
     */
    List<CouponDetail> findAllByCustomerAndCouponStatus(Customer customer,String status);

    /**
     * 查询会员指定优惠券码的未使用的优惠券
     * @param customer
     * @param codes
     * @return
     */
    @Query(value = "select c from CouponDetail c where c.customer = ?1 and c.couponCode in ?2 and (c.isUsed is null or c.isUsed=false )")
    List<CouponDetail> findAllByCustomerAndCouponCodeIn(Customer customer,Set<Long>codes);

    /**
     * 查询会员已使用指定优惠券类型的所有记录
     * @param customer
     * @param isUsed
     * @param coupon
     * @return
     */
    List<CouponDetail> findAllByCustomerAndIsUsedAndCoupon(Customer customer,Boolean isUsed,Coupon coupon);

    /**
     * 查询会员某优惠券领取数量
     * @param customer
     * @param coupon
     * @return
     */
    Integer countByCustomerAndCoupon(Customer customer,Coupon coupon);

    /**
     * 查询会员某优惠券指定状态的领取数量
     * @param customer
     * @param coupon
     * @param status
     * @return
     */
    Integer countByCustomerAndCouponAndCouponStatus(Customer customer,Coupon coupon,String status);

    /**
     * 回滚优惠券
     * @param detailId
     * @return
     */
    @Modifying
    @Transactional
    @Query(value = "UPDATE COUPON_DETAIL SET COUPON_STATUS='UNUSED',IS_USED=0,CONSUMED_TIME=NULL WHERE COUPON_DETAIL_ID = ?1 AND IS_USED= 1",nativeQuery = true)
    int rollbackCouponDetailIsUnused(Long detailId);
    /**
     * 使用优惠券
     * @param detailId
     * @return
     */
    @Modifying
    @Transactional
    @Query(value = "UPDATE COUPON_DETAIL SET COUPON_STATUS='USED',IS_USED=1,CONSUMED_TIME=sysdate() WHERE COUPON_DETAIL_ID = ?1 AND IS_USED= 0",nativeQuery = true)
    int setCouponDetailIsUsed(Long detailId);

    /**
     * 根据核销码、门店查询优惠券
     * @param code
     * @return
     */
    CouponDetail findFirstByConsumeCodeAndReleaseBy(String code,MerchantStore store);

    /**
     * 查询用户指定优惠券
     * @param id
     * @param customer
     * @return
     */
    CouponDetail findFirstByCustomerAndCouponDetailId(Customer customer,Long id);


    /**
     * 根据核销码查询优惠券
     * @param code
     * @return
     */
    CouponDetail findFirstByConsumeCode(@Nonnull  String  code);

    CouponDetail findByCouponCode(Long couponCode);

    /**
     * 退还优惠券
     * @param couponCode
     * @return
     */
    @Modifying
    @Transactional
    @Query(value = "UPDATE COUPON_DETAIL SET CUSTOMER IS NULL WHERE COUPON_CODE = ?1 AND IS_USED= 0",nativeQuery = true)
    void setCouponDetailCustomerIsNull(String couponCode);

    /**
     * 获取已生成但未被销售的优惠券明细
     * @return
     * @param coupon
     */
    @Query(value = "SELECT * FROM COUPON_DETAIL WHERE COUPON_ID = ?1 AND CUSTOMER_ID IS NULL",nativeQuery = true)
    List<CouponDetail> findAllByCouponAndCustomerIsNull(Long coupon);

    @Query(value = "UPDATE COUPON_DETAIL SET CONSUME_CODE=?3,GENERATED_TIME =?4 WHERE COUPON_DETAIL_ID=?1 AND CUSTOMER_ID=?2" , nativeQuery = true)
    @Modifying
    @Transactional
    int updateConsumeCode(Long couponDetailId ,Long customerId, String consumerCode, Date generatedTime);
}

